package com.eims.sls_app.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import bls.juejin.cn.R;
import com.eims.sls_app.dataaccess.Schemes;
import com.eims.sls_app.view.MyListView2;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

public class CenterFollowLotteryAdapter extends BaseAdapter {

	private Context mContext;
	private HashMap<String, List<String>> mMap;

	private List<String> dath;
	private List<List<Schemes>> mList;

	private OnItemClickListener listener;

	public CenterFollowLotteryAdapter(Context context, List<String> list,
			List<List<Schemes>> mlist, OnItemClickListener listener) {
		this.mContext = context;
		this.listener = listener;
		setDate(list, mlist);
	}

	public void setDate(List<String> list, List<List<Schemes>> mlist) {
		if (null == dath)
			dath = new ArrayList<String>();
		if (null == mList)
			mList = new ArrayList<List<Schemes>>();
		dath.clear();
		mList.clear();

		for (String str : list)
			dath.add(str);

		for (List<Schemes> li : mlist) {
			List<Schemes> l = new ArrayList<Schemes>();
			for (Schemes str : li) {
				l.add(str);
			}

			mList.add(l);
		}
	}

	/** 给集合赋值 */
	public void setHashMap(HashMap<String, List<String>> set) {
		for (Entry<String, List<String>> entry : set.entrySet()) {
			List<String> l = new ArrayList<String>();
			for (String s : entry.getValue()) {
				l.add(s);
			}
			this.mMap.put(entry.getKey(), l);
		}
	}

	@Override
	public int getCount() {
		return dath.size();
	}

	@Override
	public Object getItem(int arg0) {
		return dath.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder;
		if (null == view) {
			LayoutInflater inflact = LayoutInflater.from(mContext);
			view = inflact.inflate(R.layout.center_lottery_item, null);
			holder = new ViewHolder();
			holder.tv_month = (TextView) view.findViewById(R.id.tv_month);
			holder.tv_day = (TextView) view.findViewById(R.id.tv_day);
			holder.listView = (MyListView2) view
					.findViewById(R.id.item_listView);
			view.setTag(holder);
		} else {
			holder = (ViewHolder) view.getTag();
		}

		holder.listView.setAdapter(new MyListViewAdapter(mList.get(position),
				position));
		holder.listView.setOnItemClickListener(listener);

		holder.tv_month.setText(dath.get(position).split("-")[1] + "月");
		holder.tv_day.setText(dath.get(position).split("-")[2]);

		return view;
	}

	static class ViewHolder {
		TextView tv_month, tv_day;
		MyListView2 listView;
	}

	class MyListViewAdapter extends BaseAdapter {
		private List<Schemes> aList;
		private int id;

		public MyListViewAdapter(List<Schemes> aList, int id) {
			this.aList = aList;
			this.id = id;
		}

		@Override
		public int getCount() {
			return aList.size();
		}

		@Override
		public Object getItem(int position) {
			return aList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder2 holder;
			if (null == convertView) {
				LayoutInflater inflact = LayoutInflater.from(mContext);
				convertView = inflact.inflate(
						R.layout.center_lottery_item_item, null);
				holder = new ViewHolder2();
				holder.tv_name = (TextView) convertView
						.findViewById(R.id.tv_lotteryName);
				holder.tv_money = (TextView) convertView
						.findViewById(R.id.tv_money);
				holder.tv_type = (TextView) convertView
						.findViewById(R.id.tv_playType);
				holder.tv_win = (TextView) convertView
						.findViewById(R.id.tv_isWin);
				holder.tv_id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.img_logo = (ImageView) convertView
						.findViewById(R.id.img_win);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder2) convertView.getTag();
			}

			holder.tv_id.setText(id + "");
			holder.tv_id.setVisibility(View.GONE);
			holder.img_logo.setVisibility(View.GONE);

			Schemes scheme = aList.get(position);

			holder.tv_name.setText(scheme.getLotteryName());
			holder.tv_money.setText(scheme.getMoney() + "元");

			String status = scheme.getSchemeStatus();
			if (status.equals("已中奖")) {
				holder.tv_win.setTextColor(Color.RED);
				holder.img_logo.setVisibility(View.VISIBLE);
				holder.tv_win.setText("中奖" + scheme.getWinMoneyNoWithTax()
						+ "元");
			} else {
				holder.tv_win.setText(status);
			}

			/** 合买订单状态 */
			if ("False".equals(scheme.getIsPurchasing())) {
				holder.tv_type.setText("合买订单");
			} else if ("True".equals(scheme.getIsPurchasing())) {
				/** 普通订单状态 */
				if (scheme.getIsChase() == 0) {
					holder.tv_type.setText("普通订单");
				} else {
					/** 追号订单状态 */
					holder.tv_type.setText("追号订单");
				}
			}

			return convertView;
		}
	}

	static class ViewHolder2 {
		TextView tv_name, tv_money, tv_type, tv_win, tv_id;
		ImageView img_logo;
	}

}
